package com.scau.ums.controller;

import com.scau.ums.mapper.MaintainerMapper;
import com.scau.ums.mapper.StudentMapper;
import com.scau.ums.mapper.TeacherMapper;
import com.scau.ums.model.Maintainer;
import com.scau.ums.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author xfy
 * @version 1.0
 * @date 2019/8/5 17:25
 */

@Controller
public class DeleteUserController {
    @Autowired
    public StudentMapper studentMapper;
    @Autowired
    public TeacherMapper teacherMapper;
    @Autowired
    public MaintainerMapper maintainerMapper;

    @RequestMapping("/deleteUser")
    @ResponseBody
    public String deleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userType = request.getParameter("userType");
        String id = request.getParameter("id");
        System.out.println("用户类型：" + userType + " id：" + id);

        try {
            switch (userType){
                case "student":
                    studentMapper.deleteByPrimaryKey(id);
                    break;
                case "teacher":
                    teacherMapper.deleteByPrimaryKey(id);
                    break;
                case "maintainer":
                    maintainerMapper.deleteByPrimaryKey(id);
                    break;
                    default:
                        throw new Exception();
            }
        }catch (Exception e){
            System.out.println(e.getStackTrace());
            return "deleteFailed";
        }
        return "deleteSuccessfully";
    }
}
